/*
 * Game1Operator.java
 *
 * Created on 2006. december 5., 19:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hu.de.game1;

import hu.de.ai.games.*;

public class Game1Operator extends GameOperator{
    
    public static final int DIR_NW = 0;
    public static final int DIR_SW = 1;
    public static final int DIR_NE = 2;
    public static final int DIR_SE = 3;
    public int direction, column, row;
    
    public Game1Operator(String direction, int column, int row) {
        this(-1, column, row);
        if("NW".equals(direction)) {
            this.direction = DIR_NW;
        } else if("SW".equals(direction)) {
            this.direction = DIR_SW;
        } else if("NE".equals(direction)) {
            this.direction = DIR_NE;
        } else if("SE".equals(direction)) {
            this.direction = DIR_SE;
        }
    }

    public Game1Operator(int direction, int column, int row) {
        this.direction = direction;
        this.column = column;
        this.row = row;
    }

    public boolean isUsable(GameState state) {
        Game1State gstate = (Game1State)state;
        if(gstate.getField(column, row) != state.player) return false;
        int targetFieldColumn = column;
        int targetFieldRow = row;
        if(direction == DIR_NE || direction == DIR_NW)
            targetFieldRow--;
        else
            targetFieldRow++;
        if(direction == DIR_SE || direction == DIR_NE)
            targetFieldColumn++;
        else
            targetFieldColumn--;
        if(targetFieldColumn < 0 || targetFieldColumn > 4 || targetFieldRow < 0 || targetFieldRow > 4 || gstate.getField(targetFieldColumn, targetFieldRow) != 0)
            return false;
        return true;
        
    }

    public int cost() {
        return 1;
    }

    public String toString() {
        StringBuffer buffer = new StringBuffer("");
        switch(direction) {
            case DIR_NE: 
                buffer.append("NE");
                break;
            case DIR_NW:
                buffer.append("NW");
                break;
            case DIR_SE:
                buffer.append("SE");
                break;
            case DIR_SW:
                buffer.append("SW");
                break;
        }
        buffer.append(", ").append(column + 1).append(", ").append(row + 1);
        return buffer.toString();
    }
}
